<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/12/3
 * Time: 15:56
 */

namespace app\common\mysic;
use app\common\library\MysicLog;
use app\common\library\wechat\WxPay;
use app\common\model\dealer\Capital;
use app\common\model\divide\Receive;
use app\common\model\divide\ReceiveUser;
use app\common\model\Order;
use app\common\model\User;
use app\common\model\dealer\User as DealUser;
use app\common\model\Wxsetting;
use app\common\mysic\bonus\BonusReceiveOp;
use app\common\mysic\bonus\BonusReceiveUserOp;
use app\common\mysic\user\DealUserAccountOp;
use app\common\mysic\WxError\LowRefundFee;
use app\common\mysic\WxError\RefundFullyBalance;
use app\common\mysic\WxError\RefundTimesExceeded;

class WxRefundErrorResponseHandle
{
    /**
     * @var int
     */
    protected $bonus;
    /**
     * @var Capital
     */
    protected $dealUserCapitalModel;
    /**
     * @var User
     */
    protected $userModel;
    /**
     * @var ReceiveUser
     */
    protected $bonusReceiveUserModel;
    /**
     * @var DealUser
     */
    protected $dealUserEntity;
    /**
     * @var Receive
     */
    protected $receivesEntity;
    /**
     * @var Wxsetting
     */
    protected $bonusSettingEntity;
    /**
     * @var Order
     */
    protected $paidUserOrderEntity;
    /**
     * @var Order
     */
    protected $refundOrderEntity;
    /**
     * @var DealUserAccountOp
     */
    protected $dealUserAccountOp;
    /**
     * @var BonusReceiveOp
     */
    protected $bonusReceiveOp;
    /**
     * @var BonusReceiveUserOp
     */
    protected $bonusReceiveUserOp;
    /**
     * @var MysicLog|null
     */
    protected $log;
    /**
     * @var WxPay
     */
    protected $wxApi;

    protected $errors = [
        '同一笔订单的退款数量超过限制',
        '订单已全额退款',
        'refund_fee大于可退金额'
    ];
    protected $handler = null;
    protected $handlers = [];
    public function __construct()
    {
        $this->log = MysicLog::instance();
    }
    public function setWxApi($wxApi)
    {
        $this->wxApi = $wxApi;
        return $this;
    }
    public function setPaidUserOrderEntity($paidUserOrderEntity)
    {
        $this->paidUserOrderEntity = $paidUserOrderEntity;
        return $this;
    }
    public function setBonusSettingEntity($bonusSettingEntity)
    {
        $this->bonusSettingEntity = $bonusSettingEntity;
        return $this;
    }
    public function setUserModel($userModel)
    {
        $this->userModel = $userModel;
        return $this;
    }
    public function setBonusReceiveUserModel($bonusReceiveUserModel)
    {
        $this->bonusReceiveUserModel = $bonusReceiveUserModel;
        return $this;
    }
    public function setReceivesEntity($receivesEntity)
    {
        $this->receivesEntity = $receivesEntity;
        return $this;
    }
    public function setDealUserEntity($dealUserEntity)
    {
        $this->dealUserEntity = $dealUserEntity;
        return $this;
    }
    public function setRefundOrderEntity($refundOrderEntity)
    {
        $this->refundOrderEntity = $refundOrderEntity;
        return $this;
    }
    public function setBonus($bonus)
    {
        $this->bonus = $bonus;
        return $this;
    }
    public function setUserCapitalModel($userCapitalModel)
    {
        $this->dealUserCapitalModel = $userCapitalModel;
        return $this;
    }
    public function setBonusReceiveUserOp($bonusReceiveUserOp)
    {
        $this->bonusReceiveUserOp = $bonusReceiveUserOp;
        return $this;
    }
    public function setBonusReceiveOp($bonusReceiveOp)
    {
        $this->bonusReceiveOp = $bonusReceiveOp;
        return $this;
    }
    public function setDealUserAccountOp($dealUserAccountOp)
    {
        $this->dealUserAccountOp = $dealUserAccountOp;
        return $this;
    }
    public function instance($errorMsg)
    {
        if(!in_array($errorMsg, $this->errors)) {
            return null;
        }
        switch($errorMsg) {
            case '同一笔订单的退款数量超过限制':
                if(!key_exists(RefundTimesExceeded::class, $this->handlers)) {
                    $this->handlers[RefundTimesExceeded::class] = new RefundTimesExceeded();
                }
                $this->handler = $this->handlers[RefundTimesExceeded::class];
                break;
            case '订单已全额退款':
                if(!key_exists(RefundFullyBalance::class, $this->handlers)){
                    $this->handlers[RefundFullyBalance::class] = new RefundFullyBalance();
                }
                $this->handler = $this->handlers[RefundFullyBalance::class];
                break;
            case 'refund_fee大于可退金额':
                if(!key_exists(LowRefundFee::class, $this->handlers)) {
                    $this->handlers[LowRefundFee::class] = new LowRefundFee();
                }
                $this->handler = $this->handlers[LowRefundFee::class];
                break;
            default:
                return null;
        }
        return $this->handler;
    }
    public function isHandleableError($error)
    {
        return in_array($error, $this->errors);
    }
}